<?php

class SettingManager extends TModule
{
	static $CONFERENCE_NAME	= 'CONFERENCE_NAME';
	static $MAIL			= 'MAIL';
	static $MAIL_PASSWORD	= 'MAIL_PASSWORD';
	static $MAIL_USERNAME	= 'MAIL_USERNAME';
	static $MAIL_SERVER		= 'MAIL_SERVER';
	
	static $EARLY_REGISTRATION_DATE = 'EARLY_REGISTRATION_DATE';
	static $CURRENCY_NAME			= 'CURRENCY_NAME';
	
	// -------------------- basic functions --------------------
	public function addSetting($name, $value)
	{
		$setting = new SettingRecord();
		$setting->setData($name, $value);
		
		if($setting->save())
			return $setting;
		else
			return null;
	}
	
	public function deleteSetting($name)
	{
		return SettingRecord::finder()->deleteByName($name);
	}
	
	public function updateSetting($name, $value)
	{	
		$settingRecord = new SettingRecord();
		$settingRecord->setData($name, $value);
		return $this->updateSettingBySettingRecord($settingRecord);
	}

	public function updateSettingBySettingRecord($settingRecord)
	{	
		if($settingRecord instanceof SettingRecord)
		{
			$finder = SettingRecord::finder();
			$name = $settingRecord->getName();
			if($name != null && $rec = $finder->findByPk($name))
			{
				if($rec->equals($settingRecord))
					return $rec;
				$rec->setDataBySettingRecord($settingRecord);
				if($rec->save())
					return $rec;
			}
		}
		return null;
	}
	
	public function getSettingByName($name)
	{
		return SettingRecord::finder()->findByName($name);
	}
	
	// -------------------- extended functions --------------------
	public function getSettingsList($revert = false, $offset = -1, $limit = -1)
	{
		$criteria = new TActiveRecordCriteria();
		
		if($revert)
			;//$criteria->OrdersBy['registration'] = 'desc';
			
		if($offset < 0 || $offset == null)
			$offset = 0;
		if($limit < 0 || $limit == null)
			$limit = $this->countSettings();
			
		$criteria->Offset = $offset;
		$criteria->Limit = $limit;
		
		return SettingRecord::finder()->findAll($criteria);
	}
	
	public function countSettings()
	{
		return SettingRecord::finder()->count();
	}
}

?>